package com.kaka.bus.engine.home.info.self;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.User;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.user.login.LoginActivity;
import com.kaka.bus.listener.Listener;
import com.kaka.bus.util.U;
import com.kaka.bus.util.UserUtil;
import com.kaka.bus.view.SelfItemView;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/10/31.
 */

public class SelfActivity extends BaseActivity<SelfPresenter> implements SelfContract.View {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.self_swipe)
    SwipeRefreshLayout mSelfSwipe;
    @BindView(R.id.self_idcard)
    SelfItemView mSelfIdcard;
    @BindView(R.id.self_name)
    SelfItemView mSelfName;
    @BindView(R.id.self_phone)
    SelfItemView mSelfPhone;
    @BindView(R.id.self_job)
    SelfItemView mSelfJob;
    @BindView(R.id.self_income)
    SelfItemView mSelfIncome;
    @BindView(R.id.self_contact)
    TextView mSelfContact;
    @BindView(R.id.self_contact1)
    TextView mSelfContact1;
    @BindView(R.id.self_contact2)
    TextView mSelfContact2;
    @BindView(R.id.self_btn)
    AppCompatButton mSelfBtn;

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("个人资料");

        mSelfSwipe.setColorSchemeColors(getResources().getColor(R.color.toolbar_color));
        mSelfSwipe.setRefreshing(true);
    }

    @Override
    protected void initData() {
        mPresenter.loadSelfInfo(getUserId(), getToken());
    }

    @Override
    public void loadSuccess(HttpWrapper<User> wrapper) {
        closeRefresh();
        if (Constant.SUCCESS_CODE.equals(wrapper.getCode())) {
            showInfo(wrapper.getData());
        } else {
            showMessage(wrapper.getInfo());
        }
    }

    @Override
    public void onFinish() {
        closeRefresh();
    }

    private void showInfo(User data) {

        if (data == null) {
            return;
        }
        /**
         * 身份证打码
         */
        String cardNum = data.getCardNum();
        if (TextUtils.isEmpty(cardNum)) {
            cardNum = "暂无";
        }
        if (cardNum.length() == 15) {
            cardNum = cardNum.substring(0, 2) + "********" + cardNum.substring(11, cardNum.length());
        } else if (cardNum.length() == 18) {
            cardNum = cardNum.substring(0, 2) + "********" + cardNum.substring(14, cardNum.length());
        }

        mSelfIdcard.setValue("身份证号码", cardNum);
        mSelfName.setValue("真实姓名", data.getName());
        mSelfPhone.setValue("手机号码", data.getPhone());
        mSelfIncome.setValue("收入水平", data.getMonthlypay());
        mSelfJob.setValue("职业", data.getProfession());
        String relatives = data.getRelatives();
        if (relatives == null) {
            relatives = "";
        }
        switch (relatives) {
            case "0":
                relatives = "夫妻";
                break;
            case "1":
                relatives = "父母";
                break;
            case "2":
                relatives = "兄弟姐妹";
                break;
            case "3":
                relatives = "子女";
                break;
            default:
                return;
        }

        mSelfContact1.setText(U.connectString(data.getRelativesName(), "(", relatives, ")", data.getRelaPhone()));

        String friend = data.getSociety();
        if (friend == null) {
            friend = "";
        }
        switch (friend) {
            case "4":
                friend = "同事";
                break;
            case "5":
                friend = "同学";
                break;
            case "6":
                friend = "朋友";
                break;
            default:
                return;
        }

        mSelfContact2.setText(U.connectString(data.getSocietyName(), "(", friend, ")", data.getSociPhone()));

    }

    @Override
    protected void initListener() {
        mSelfBtn.setOnClickListener(v -> {
            U.getSlectDialog(this, "温馨提示", "您确定要退出登录吗?", "取消", "退出",
                    new Listener.OnNormalAlertDialogChooseClickListener() {
                        @Override
                        public void onLeft() {

                        }

                        @Override
                        public void onRight() {
//                            mPresenter.unregisterPush(getUserId());
                            setUserInfo(null);
                            Intent intent = new Intent(SelfActivity.this, LoginActivity.class);
                            startActivity(intent);
                            UserUtil.setCache(UserUtil.PERSONINFO, null);
                            AppCenter.cloaseApp();
                        }
                    }).show();
        });

        mSelfSwipe.setOnRefreshListener(this::initData);
    }

    public void closeRefresh() {
        if (mSelfSwipe == null) {
            return;
        }

        if (mSelfSwipe.isRefreshing()) {
            mSelfSwipe.setRefreshing(false);
        }
    }


    @Override
    protected boolean needFullScreen() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_self;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
